import 'package:flutter/material.dart';
import 'common_image.dart';
import 'tag.dart';

/// 资讯数据模型
class NewsItem {
  final String id;
  final String imageUrl;
  final String title;
  final String description;
  final String tag;
  final DateTime date;

  NewsItem({
    required this.id,
    required this.imageUrl,
    required this.title,
    required this.description,
    required this.tag,
    required this.date,
  });
}

/// 资讯卡片组件
class NewsCard extends StatelessWidget {
  final NewsItem newsItem;
  final VoidCallback? onTap;

  const NewsCard({
    Key? key,
    required this.newsItem,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(4),
      ),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(4),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 图片部分
            ClipRRect(
              borderRadius: const BorderRadius.vertical(
                top: Radius.circular(4),
              ),
              child: AspectRatio(
                aspectRatio: 16 / 9, // 图片宽高比
                child: CommonImage(
                  src: newsItem.imageUrl,
                  fit: BoxFit.cover,
                  placeholder: Container(
                    color: Colors.grey[200],
                    child: const Center(
                      child: Icon(
                        Icons.image,
                        color: Colors.grey,
                        size: 40,
                      ),
                    ),
                  ),
                ),
              ),
            ),
            // 内容部分
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(12),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 标题 - 占一行
                    Text(
                      newsItem.title,
                      style: const TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        height: 1.2,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 6),
                    // 描述 - 占一行
                    Expanded(
                      child: Text(
                        newsItem.description,
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey[600],
                          height: 1.3,
                        ),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const SizedBox(height: 8),
                    // 标签和日期 - 占一行
                    Row(
                      children: [
                        // 标签
                        Tag.auto(newsItem.tag),
                        const Spacer(),
                        // 日期
                        Text(
                          _formatDate(newsItem.date),
                          style: TextStyle(
                            fontSize: 10,
                            color: Colors.grey[500],
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 格式化日期显示
  String _formatDate(DateTime date) {
    final now = DateTime.now();
    final difference = now.difference(date);

    if (difference.inDays == 0) {
      return '今天';
    } else if (difference.inDays == 1) {
      return '昨天';
    } else if (difference.inDays < 7) {
      return '${difference.inDays}天前';
    } else {
      return '${date.month}-${date.day}';
    }
  }
}
